package com.srxz.personnelsystem.util;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.StringUtils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @作者：wgchao
 * @时间：19:24 2019/1/22 0022
 * @用途：
 **/
@Data
@AllArgsConstructor
@NoArgsConstructor
public class BackMessage implements Serializable {
    private int code;
    private String msg;
    private Map<String, Object> mdata;
    private Object data;

    //分页查询数据
    private long total;//数据总数
    private List rows;//分页数据

    public BackMessage(int code, String msg, Map<String, Object> mdata, Object data){
        this.code = code;
        this.msg = msg;
        this.mdata = mdata;
        this.data = data;
    }

    public BackMessage putPageData(long total, List rows){
        this.rows = rows;
        this.total = total;
        return this;
    }

    public static BackMessage success(){
        return new BackMessage(200, "successful", null, null);
    }

    public BackMessage putData(Map data){
        this.data = data;
        return this;
    }

    public BackMessage putData(String key, Object data){
        if(this.mdata == null){
            this.mdata = new HashMap<>();
        }
        this.mdata.put(key, data);
        return this;
    }

    public BackMessage putData(Object data){
        this.data = data;
        return this;
    }

    public static BackMessage error(String msg){
        if(StringUtils.isEmpty(msg))
            msg = "error";

        return new BackMessage(999, msg, null, null);
    }
}
